import Vue from "vue";
import Transaction from "./index.vue";

export default class Buy {
  constructor(goodsInfo = {}, jumpDataCustom = {}, isNeedHB = false) {
    this.goodsInfo = goodsInfo;
    this.jumpDataCustom = jumpDataCustom;
    this.isNeedHB = isNeedHB;
  }

  /**
   * 获取支付方式
   * @returns 返回一个对象
   * { token, payWay }：payWay: 支付方式 token: 批价接口返回的token信息
   */
  getPayWay() {
    let promiseResolve
    const resultPromise = new Promise((resolve) => {
      promiseResolve = resolve;
    })
    const TransactionConstructor = Vue.extend(Transaction);
    const instance = new TransactionConstructor({
      propsData: {
        goodsInfo: this.goodsInfo,
        jumpDataCustom: this.jumpDataCustom,
        isNeedHB: this.isNeedHB,
        promiseResolve,
      },
    });
    instance.$mount();
    instance.initData();
    document.body.appendChild(instance.$el);

    return resultPromise
  }

  /**
   * 下单
   * @param {*} data payWay: 支付方式 token: 批价接口返回的token信息
   */
  perform(data = {}) {
    const TransactionConstructor = Vue.extend(Transaction);
    const instance = new TransactionConstructor({
      propsData: {
        goodsInfo: this.goodsInfo,
        jumpDataCustom: this.jumpDataCustom,
        isNeedHB: this.isNeedHB,
      },
    });
    instance.$mount();
    const buyPromise = instance.buy(data);
    document.body.appendChild(instance.$el);

    return buyPromise;
  }
}
